package com.hw.one.dam2.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.entity.PollutionIndustryEntity;
import com.hw.one.dam2.param.PollutionIndustryParam;

@Repository(value = "waterPollutionDao")
public class WaterPollutionDao implements IWaterPollutionDao {

	// 在Spring中注册的 mongoDB Template
	@Autowired
	private MongoTemplate mongoTemplateStandardSiteData;

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findWaterPollutionNewHourDataByPSCodeAndOutputCodes(List<String> siteCodeList) throws Exception {

		Map newData = mongoTemplateStandardSiteData.findOne(
				new Query(Criteria.where("PSCode").in(siteCodeList)).with(new Sort(Sort.Direction.DESC, "MONITORTIME")),
				Map.class, "T_DATA_WATERPOLLUTION_HOUR_S_COLLECT");

		Object timeObj = newData.get("MONITORTIME");
		SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
		Date date = sdf.parse(timeObj.toString());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.add(Calendar.HOUR_OF_DAY, -1);

		String startTime = sdf.format(calendar.getTime());

		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME").is(startTime);
		criteria.and("PSCode").in(siteCodeList);

		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));

		List<Map> ipList = mongoTemplateStandardSiteData.find(query, Map.class, "T_DATA_WATERPOLLUTION_HOUR_S_COLLECT");

		return ipList;
	}

	@Override
	public List<PollutionIndustryEntity> findWaterPollutionDataByPSCode(PollutionIndustryParam paramPO,
			String collectionName) throws Exception {
		String startTime = paramPO.getStartTime();
		String endTime = paramPO.getEndTime();
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("MONITORTIME");

		criteria.gte(startTime).lte(endTime);
		criteria.and("PSCode").in(paramPO.getPSCodeList());

		// 创建查询对象
		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
		List<PollutionIndustryEntity> ipList = mongoTemplateStandardSiteData.find(query, PollutionIndustryEntity.class,
				collectionName);

		return ipList;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<PollutionIndustryEntity> findWaterPollutionLast12HourDataBySiteCode(String siteCode) {
		// 查询条件 增加时间查询条件
		Criteria criteria = Criteria.where("PSCode").is(siteCode);

		// 创建查询对象
		Query query = new Query(criteria);
		query.with(new Sort(Sort.Direction.DESC, "MONITORTIME")).limit(12);
		List<Map> ipList = mongoTemplateStandardSiteData.find(query, Map.class,
				"T_DATA_WATERPOLLUTION_HOUR_S_COLLECT");
		
		// 把时间顺序正序排序
		Collections.reverse(ipList);
		List<PollutionIndustryEntity> wpList = new ArrayList<PollutionIndustryEntity>();
		
		PollutionIndustryEntity dataPO = null;
		for(Map dataMap: ipList){
			Object MONITORTIMEObj = dataMap.get("MONITORTIME");
			if(null==MONITORTIMEObj) {
				continue;
			}
			
			dataPO = new PollutionIndustryEntity();
			
			Object PSCodeObj = dataMap.get("PSCode");
			dataPO.setPSCode(PSCodeObj.toString());
			
			dataPO.setMonitorTime(MONITORTIMEObj.toString());
			
			Object PFL001Obj = dataMap.get("PFL001");
			PFL001Obj = null == PFL001Obj || "".equals(PFL001Obj) ? "-" : PFL001Obj;
			dataPO.setPFL001(PFL001Obj.toString());
			
			Object PFL002Obj = dataMap.get("PFL002");
			PFL002Obj = null == PFL002Obj || "".equals(PFL002Obj) ? "-" : PFL002Obj;
			dataPO.setPFL002(PFL002Obj.toString());
			
			Object PFL003Obj = dataMap.get("PFL003");
			PFL003Obj = null == PFL003Obj || "".equals(PFL003Obj) ? "-" : PFL003Obj;
			dataPO.setPFL003(PFL003Obj.toString());
			
			Object AvgStrength001Obj = dataMap.get("AvgStrength001");
			AvgStrength001Obj = null == AvgStrength001Obj || "".equals(AvgStrength001Obj) ? "-" : AvgStrength001Obj;
			dataPO.setAvgStrength001(AvgStrength001Obj.toString());
			
			Object AvgStrength002Obj = dataMap.get("AvgStrength002");
			AvgStrength002Obj = null == AvgStrength002Obj || "".equals(AvgStrength002Obj) ? "-" : AvgStrength002Obj;
			dataPO.setAvgStrength002(AvgStrength002Obj.toString());
			
			Object AvgStrength003Obj = dataMap.get("AvgStrength003");
			AvgStrength003Obj = null == AvgStrength003Obj || "".equals(AvgStrength003Obj) ? "-" : AvgStrength003Obj;
			dataPO.setAvgStrength003(AvgStrength003Obj.toString());
			
			Object CouFlowObj = dataMap.get("CouFlow");
			CouFlowObj = null == CouFlowObj || "".equals(CouFlowObj) ? "-" : CouFlowObj;
			dataPO.setCouFlow(CouFlowObj.toString());
			
			Object AvgFlowObj = dataMap.get("AvgFlow");
			AvgFlowObj = null == AvgFlowObj || "".equals(PFL001Obj) ? "-" : AvgFlowObj;
			dataPO.setAvgFlow(AvgFlowObj.toString());
			
			Object OutputCodeObj = dataMap.get("OutputCode");
			OutputCodeObj = null == OutputCodeObj ? "" : OutputCodeObj;
			dataPO.setOutputCode(OutputCodeObj.toString());
			
			wpList.add(dataPO);
		}
		return wpList;
	}

}
